package com.example.micro_property_springboot.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.micro_property_springboot.pojo.AssetInfo;
import com.example.micro_property_springboot.pojo.DTO.request.AssetInfoRequest;
import com.example.micro_property_springboot.pojo.VO.PageResult;
import com.example.micro_property_springboot.pojo.VO.Result;
import com.example.micro_property_springboot.service.AssetInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/assetInfo")
@Tag(name = "资产信息接口", description = "资产信息相关操作")
public class AssetInfoController {

    @Autowired
    private AssetInfoService assetInfoService;

    /**
     * 创建资产信息
     */
    @PostMapping("/create")
    @Operation(summary = "创建资产信息", description = "根据资产信息请求参数创建资产信息")
    public Result<AssetInfo> createAssetInfo(@RequestBody AssetInfoRequest request) {
        AssetInfo assetInfo = assetInfoService.createAssetInfo(request);
        return Result.success("创建成功", assetInfo);
    }

    /**
     * 更新资产信息
     */
    @PostMapping("/update/{id}")
    @Operation(summary = "更新资产信息", description = "根据资产信息ID和请求参数更新资产信息")
    public Result<Boolean> updateAssetInfo(@PathVariable Long id, @RequestBody AssetInfoRequest request) {
        boolean updated = assetInfoService.updateAssetInfo(id, request);
        return updated ? Result.success("更新成功", true) : Result.error("更新失败");
    }

    /**
     * 删除资产信息
     */
    @PostMapping("/delete/{id}")
    @Operation(summary = "删除资产信息", description = "根据资产信息ID删除资产信息")
    public Result<Boolean> deleteAssetInfo(@PathVariable Long id) {
        boolean deleted = assetInfoService.deleteAssetInfo(id);
        return deleted ? Result.success("删除成功", true) : Result.error("删除失败");
    }

    /**
     * 查询资产信息
     */
    @GetMapping("/query/{id}")
    @Operation(summary = "查询资产信息", description = "根据资产信息ID查询资产信息")
    public Result<AssetInfo> queryAssetInfo(@PathVariable Long id) {
        AssetInfo assetInfo = assetInfoService.getById(id);
        return assetInfo != null ? Result.success("查询成功", assetInfo) : Result.error("资产信息不存在");
    }

    /**
     * 批量查询资产信息
     */
    @GetMapping("/query/batch")
    @Operation(summary = "批量查询资产信息", description = "根据资产状态批量查询资产信息")
    public Result<List<AssetInfo>> queryAssetInfoList(@RequestParam String status) {
        List<AssetInfo> assetInfoList = assetInfoService.getAssetsByStatus(status);
        return Result.success("查询成功", assetInfoList);
    }

    /**
     * 分页+条件查询资产信息
     */
    @GetMapping("/query/page")
    @Operation(summary = "分页+条件查询资产信息", description = "根据页码、每页数量、资产名称、资产状态等条件分页查询资产信息")
    public Result<PageResult<AssetInfo>> queryAssetInfoPage(
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize,
            @RequestParam(required = false) String assetName,
            @RequestParam(required = false) String assetStatus) {

        // 设置分页参数 - 使用MyBatis-Plus的Page类
        Page<AssetInfo> page = new Page<>(pageNum, pageSize);

        // 设置条件查询
        QueryWrapper<AssetInfo> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(assetName), "name", assetName)
                .eq(StringUtils.isNotBlank(assetStatus), "status", assetStatus);

        // 执行查询
        IPage<AssetInfo> assetInfoPage = assetInfoService.page(page, wrapper);

        // 转换为自定义的PageResult
        PageResult<AssetInfo> pageResult = new PageResult<>();
        pageResult.setTotal(assetInfoPage.getTotal());
        pageResult.setRows(assetInfoPage.getRecords());

        return Result.success(pageResult);
    }
}
